package annuaire.rmi.remotedObject.impl;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import annuaire.dao.IAnnuaireDao;
import annuaire.exceptions.BusinessException;
import annuaire.exceptions.PermissionException;
import annuaire.rmi.remotedObject.IAdminService;
import annuaire.rmi.remotedObject.IRemotedUser;
import annuaire.rmi.remotedObject.IStudentService;

/**
 * Classe abstraite d'objet distant RMI, représentant un utilisateur.
 * 
 * @author Yannick Lorenzati & Stephanie Malakian
 * 
 */
public abstract class RemotedUserImpl extends UnicastRemoteObject implements
		IRemotedUser {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8653299063463253012L;

	/**
	 * @throws RemoteException
	 */
	protected RemotedUserImpl() throws RemoteException {
		super();
	}

	/**
	 * Référence vers la Dao.
	 */
	protected IAnnuaireDao dao;

	/**
	 * Donnée associée à l'utilisateur.
	 */
	protected Serializable associedBean = null;

	/**
	 * Role associé a l'utilisateur
	 */
	protected String role = null;

	/**
	 * setter de la dao
	 * 
	 * @param dao
	 */
	public void setDao(IAnnuaireDao dao) {
		this.dao = dao;
	}

	/**
	 * Setter des données associées a l'utilisateur.
	 * 
	 * @param associedBean
	 */
	public void setAssociedBean(Serializable associedBean) {
		this.associedBean = associedBean;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see rmi.remotedObject.IUser#getAdminService()
	 */
	@Override
	public IAdminService getAdminService() throws RemoteException,
			PermissionException {

		if (this instanceof IAdminService) {
			return (IAdminService) this;
		} else {
			throw new PermissionException(
					"vous n'avez pas les droits administrateur");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see rmi.remotedObject.IUser#getStudentService()
	 */
	@Override
	public IStudentService getStudentService() throws RemoteException,
			PermissionException {
		if (this instanceof IStudentService) {
			return (IStudentService) this;
		} else {
			throw new PermissionException("vous n'avez pas les droits Etudiant");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see rmi.remotedObject.IUser#logout()
	 */
	@Override
	public void logout() throws RemoteException {
		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see annuaire.rmi.remotedObject.IRemotedUser#getBeans()
	 */
	@Override
	public Serializable getBeans() throws RemoteException, BusinessException {
		return associedBean;
	}

	@Override
	public String getRole() throws RemoteException {

		return this.role;
	}

	public void setRole(String role) throws RemoteException {
		this.role = role;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * annuaire.rmi.remotedObject.IRemotedUser#updateBeans(java.io.Serializable)
	 */
	@Override
	public abstract void updateBeans(Serializable newBeans) throws Exception;

}
